<template>
  <div class="gl_contentBox gl_heightFull">
    <div class="gl_searchBox gl_mb_15">
      <el-row :gutter="20">
        <el-col :span="4">
          <p>学员</p>
          <el-input v-model="selectedMsg.StudentName" placeholder="请输入学员姓名" size="mini" clearable @change="getPageList()" />
        </el-col>
        <el-col :span="4">
          <p>状态</p>
          <el-select v-model="selectedMsg.StudentCardStatusCode" size="mini" placeholder="请选择状态" filterable @change="getPageList()">
            <el-option value="" label="全部" />
            <el-option v-for="item in dict.studentCardStatus" :key="item.code" :value="item.code" :label="item.value" />
          </el-select>
        </el-col>
        <el-col :span="4">
          <p>卡种类型</p>
          <el-select v-model="selectedMsg.ChargeTypeCode" size="mini" placeholder="请选择卡种类型" filterable @change="getPageList()">
            <el-option value="" label="全部" />
            <el-option v-for="item in dict.chargeType" :key="item.code" :value="item.code" :label="item.value + '卡'" />
          </el-select>
        </el-col>
        <el-col :span="12" class="search_btn">
          <!-- 查询按钮 -->
          <el-button type="primary" size="mini" @click="getPageList()">筛选</el-button>
          <!-- 重置按钮 -->
          <el-button type="warning" size="mini" @click="reset()">重置</el-button>
        </el-col>
      </el-row>
    </div>
    <el-table v-loading="loading" class="gl_table_noBorder" :data="tableData" size="mini">
      <el-table-column label="序号" type="index" :index="table_index" />
      <el-table-column label="卡种类型" width="90">
        <template slot-scope="{row}">
          <span>{{ chooseChargeType(row.chargeTypeCode) }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="cardNumber" label="卡种编号" min-width="150" />
      <el-table-column prop="studentName" label="学员" />
      <el-table-column label="卡内课时">
        <template slot-scope="{row}">
          <span v-if="row.chargeTypeCode === 'Date'">无限</span>
          <span v-else>{{ row.classPeriod }}</span>
        </template>
      </el-table-column>
      <el-table-column label="卡种有效期">
        <template slot-scope="{row}">
          {{ row.effectivePeriod }}
          <span v-for="item in dict.effectivePeriodType" :key="item.code">{{ row.effectivePeriodTypeCode === item.code ? item.value : '' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="剩余课时">
        <template slot-scope="{row}">
          <span v-if="row.chargeTypeCode === 'Date'">无限</span>
          <span v-else>{{ row.remainClassPeriod }}</span>
        </template>
      </el-table-column>
      <el-table-column label="到期时间" min-width="120">
        <template slot-scope="{row}">
          <span v-if="row.effectiveDate">
            {{ row.effectiveDate | FormatDate('yyyy-MM-dd') }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="状态">
        <template slot-scope="{row}">
          {{ row.studentCardStatus ? row.studentCardStatus.name : '' }}
        </template>
      </el-table-column>
      <el-table-column label="激活时间" min-width="120">
        <template slot-scope="{row}">
          <span v-if="row.enableDate">
            {{ row.enableDate | FormatDate('yyyy-MM-dd') }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="{ row }">
          <span v-if="row.studentCardStatusCode !== 'NoEnable'" class="gl_mr_10">{{ formatStatus(row.studentCardStatusCode) }}</span>
          <el-button v-else size="mini" type="text" @click="clickActivation(row)">激活</el-button>
          <el-button size="mini" type="text" @click="clickEdit(row)">编辑</el-button>
          <el-button size="mini" type="text" @click="clickOperation(row)">操作记录</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-row class="gl_mt_15" style="text-align: right;">
      <el-pagination
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="selectedMsg.MaxResultCount"
        :current-page="selectedMsg.Page"
        @size-change="changePageSize"
        @current-change="changePage"
      />
    </el-row>
    <edit-salecard ref="editSalecard" />
  </div>
</template>

<script>
import { mapState, mapActions } from 'vuex';
import editSalecard from './editSaleCard.vue';
import {
  Api_GET_ClubStudentCardsQO // 分页获取学生卡片信息数据
} from '@/lib/form/hall/swaggerForm';
import {
  api_GET_ClubStudentCards, // 分页获取学生卡片信息数据
  api_POST_ClubStudentCardsIdActivateStudentCard // 激活学生卡片
} from '@/extend/api/hallswaggerApi/app';
export default {
  components: { editSalecard },
  data () {
    return {
      selectedMsg: new Api_GET_ClubStudentCardsQO(),
      tableData: [],
      loading: false,
      total: 0
    };
  },
  computed: {
    ...mapState({
      dict: state => state.dict.dict,
      cache: state => state.dict.cache
    })
  },
  mounted() {
    if (this.selectedMsg) {
      this.selectedMsg.StudentCardStatusCode = '';
      this.selectedMsg.ChargeTypeCode = '';
    }
    this.getDict();
    this.getPageList();
  },
  methods: {
    // 若没有字典信息，外部可直接调用方法获取字典数据
    ...mapActions({ getDict: 'dict/getDict' }),
    getPageList(page) {
      this.selectedMsg.Page = page || 1;
      this.selectedMsg.SkipCount = (this.selectedMsg.Page - 1) * this.selectedMsg.MaxResultCount;
      this.selectedMsg.IncludeRegisterStudent = false;
      this.selectedMsg.IncludeStudentCardStatus = true;
      this.selectedMsg.IncludeMembershipCard = false;
      this.loading = true;
      api_GET_ClubStudentCards(this.selectedMsg).then(res => {
        this.loading = false;
        this.tableData = res.items;
        this.total = res.totalCount;
      }, error => {
        this.loading = false;
        const _msg = error.error.message || error.message;
        this.$message({ type: 'error', message: _msg });
      });
    },
    reset() {
      this.selectedMsg = new Api_GET_ClubStudentCardsQO();
      this.selectedMsg.StudentCardStatusCode = '';
      this.selectedMsg.ChargeTypeCode = '';
      this.getPageList();
    },
    // 激活
    clickActivation(row) {
      if (row.id) {
        this.loading = true;
        api_POST_ClubStudentCardsIdActivateStudentCard(row.id).then(res => {
          this.loading = false;
          this.$message({ type: 'success', message: '激活成功' });
          this.getPageList(this.selectedMsg.Page);
        }, error => {
          this.loading = false;
          const _msg = error.error.message || error.message;
          this.$message({ type: 'error', message: _msg });
        });
      }
    },
    // 编辑
    clickEdit(row) {
      this.$refs.editSalecard.dialogVisible = true;
      this.$refs.editSalecard.currentId = row.id;
      this.$set(this.$refs.editSalecard.cardForm, 'name', row.studentName);
      this.$set(this.$refs.editSalecard.cardForm, 'chargeTypeCode', row.chargeTypeCode);
      this.$set(this.$refs.editSalecard.cardForm, 'cardNumber', row.cardNumber);
      this.$refs.editSalecard.cardForm.effectiveDate = row.effectiveDate;
      this.$refs.editSalecard.cardForm.remainClassPeriod = row.remainClassPeriod;
    },
    // 处理卡片状态
    formatStatus(studentCardStatusCode) {
      let _studentCardStatus;
      if (studentCardStatusCode && this.dict && this.dict.studentCardStatus) {
        _studentCardStatus = this.dict.studentCardStatus.find(item => item.code === studentCardStatusCode);
      }
      _studentCardStatus = _studentCardStatus ? _studentCardStatus.value : '';
      return `${_studentCardStatus}`;
    },
    // 操作chargeTypeCode
    chooseChargeType(chargeTypeCode) {
      let _chargeType;
      if (chargeTypeCode && this.dict && this.dict.chargeType) {
        _chargeType = this.dict.chargeType.find(item => item.code === chargeTypeCode);
      }
      _chargeType = _chargeType ? _chargeType.value : '';
      return `${_chargeType}`;
    },
    // 操作记录
    clickOperation(row) {
      const _name = 'OperationRecord';
      this.$router.push({
        name: _name,
        params: { id: row.id }
      });
    },
    // 分页
    changePage(page) {
      this.getPageList(page);
    },
    changePageSize(pagesize) {
      this.selectedMsg.MaxResultCount = pagesize;
      this.getPageList();
    },
    // 序号递增
    table_index(index) {
      return (this.selectedMsg.Page - 1) * this.selectedMsg.MaxResultCount + index + 1;
    }
  }
};
</script>

<style lang="scss" scoped>

</style>
